package com.one.common.utils;

import android.annotation.SuppressLint;
import android.text.InputType;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.one.common.R;
import com.one.common.view.base.BaseActivity;

import io.reactivex.Observable;

/**
 * Created by yy on 2017/10/31.
 * <p>
 * EditText 联动工具类
 */

public class EditTextGangedUtils {


    /**
     * 输入框 与 发送按钮 联动（长度等于11，且显示的是获取验证码才可点击）
     *
     * @param et         输入框
     * @param tvSendCode 发送验证码
     */
    @SuppressLint("CheckResult")
    public static void setEtLengthWithSendEnable(BaseActivity mActivity, EditText et, final TextView tvSendCode) {
        RxTextView.textChanges(et)
                .compose(mActivity.<CharSequence>bindToLifecycle())
                .subscribe(charSequence -> RxView.enabled(tvSendCode).accept((charSequence.length() == 11
                        && (tvSendCode.getText().equals(ResourceUtils.getString(R.string
                        .get_verify))))));
    }

    @SuppressLint("CheckResult")
    public static void setEtLengthWithPwd(BaseActivity mActivity, EditText et, final TextView tvSendCode) {
        RxTextView.textChanges(et)
                .compose(mActivity.<CharSequence>bindToLifecycle())
                .subscribe(charSequence -> RxView.enabled(tvSendCode).accept((charSequence.length() >= 6)));
    }

    @SuppressLint("CheckResult")
    public static void setEWithImage(BaseActivity mActivity, EditText et, final ImageView imageView) {
        RxTextView.textChanges(et)
                .compose(mActivity.<CharSequence>bindToLifecycle())
                .subscribe(charSequence -> RxView.visibility(imageView).accept((charSequence.length() > 0)));
    }

    /**
     * 输入框密文明文显示切换监听
     */
    public static void setEtInputTypeWithImge(EditText et, ImageView iv) {
        if (et.getInputType() == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) {  // 目前为 可见
            iv.setImageResource(R.mipmap.ic_show_pwd);
            et.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        } else {
            iv.setImageResource(R.mipmap.ic_show_pwd_open);
            et.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
        }
        et.setSelection(et.getText().length());     //设置光标位置
    }


    @SuppressLint("CheckResult")
    public static void setEnableWithDoubleEt(BaseActivity mActivity, EditText etMobile, EditText checkCode, final
    TextView tv) {
        // 多个文本框变化监听
        Observable<CharSequence> ObservableName = RxTextView.textChanges(etMobile);
        Observable<CharSequence> ObservablePassword = RxTextView.textChanges(checkCode);
        Observable.combineLatest(ObservableName, ObservablePassword, (phoneNum, password) -> phoneNum.length() == 11 && password.length() >= 6).compose(mActivity.<Boolean>bindToLifecycle())
                .subscribe(aBoolean -> RxView.enabled(tv).accept(aBoolean));
    }

    @SuppressLint("CheckResult")
    public static void setEnableWithDoublePwd(BaseActivity mActivity, EditText etMobile, EditText checkCode, final
    TextView tv) {
        // 多个文本框变化监听
        Observable<CharSequence> ObservableName = RxTextView.textChanges(etMobile);
        Observable<CharSequence> ObservablePassword = RxTextView.textChanges(checkCode);
        Observable.combineLatest(ObservableName, ObservablePassword, (phoneNum, password) -> phoneNum.length() >= 6 && password.length() >= 6).compose(mActivity.<Boolean>bindToLifecycle())
                .subscribe(aBoolean -> RxView.enabled(tv).accept(aBoolean));
    }


}
